package models

type UserSubscriptions struct {
	ID             int    `json:"id"`
	UserId         int    `json:"user_id"`
	ProductID      int    `json:"product_id"`
	ProductFeature string `json:"product_feature"`
	Since          string `json:"since"`
	Expire         string `json:"expire"`
}

func EditUserSub(userid int, productId int, maps UserSubscriptions) bool {
	var count int64
	db.Model(&UserSubscriptions{}).Where("user_id = ? and product_id = ?", userid, productId).Count(&count)
	if maps.Since == "" {
		return true
	}
	if count > 0 {
		// 条件匹配,更新
		db.Model(&UserSubscriptions{}).Where("user_id = ? and product_id = ?", userid, productId).Update(maps)
	} else {
		maps.UserId = userid
		// 条件不匹配,插入
		db.Create(&maps)
	}
	return true
}

func GetUserSub(userid int) (device UserSubscriptions) {
	db.Where("user_id = ?", userid).Order("id desc").Find(&device)
	return
}
